<?php

/**
 * This is the model class for table "{{application}}".
 *
 * The followings are the available columns in table '{{application}}':
 * @property integer $id
 * @property string $name
 * @property string $url
 * @property string $app_id
 * @property string $token
 * @property string $authkey
 * @property string $authsecret
 * @property string $allowips
 * @property integer $is_signature
 * @property integer $is_anonymous
 * @property string $username
 * @property string $password
 * @property integer $status
 *
 *
 */

namespace  api\models;

use Yii;
use yii\data\ActiveDataProvider;
use api\components\helpers\Constant;

class Application extends \yii\db\ActiveRecord {

    const STATUS_VALID = 1;
    const STATUS_INVALID = 0;

    /**
      /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'application';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
                [['name','url'], 'required'],
                [['status','is_signature','is_anonymous'], 'integer'],
//                [['name'], 'string', 'max' => 45],
//                [['url', 'allowips'], 'string', 'max' => 128],
                [['authkey', 'authsecret','app_id','token', 'username', 'password'], 'string', 'max' => 64],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'url' => 'Url',
            'app_id' => 'APP ID',
            'token' => 'Token',
            'authkey' => 'Authkey',
            'authsecret' => 'Authsecret',
            'allowips' => 'Allowips',
            'status' => 'Status',
            'is_signature' => '是否需要代签',
            'is_anonymous' => '是否匿名登录',
            'username' => '绑定的登录账号',
            'password' => '登录密码',
        ];
    }

    /**


      /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('url', $this->url, true);
        $criteria->compare('app_id', $this->app_id, true);
        $criteria->compare('token', $this->token, true);
        $criteria->compare('authkey', $this->authkey, true);
        $criteria->compare('authsecret', $this->authsecret, true);
        $criteria->compare('allowips', $this->allowips, true);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

     /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function searchParams($params)
    {
        $query = Application::find()->orderBy('id DESC');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }

    public static function getSecret($app_id, $token) {
        $app = self::find()->where(['status' => Constant::STATUS_VALID, 'app_id' => $app_id, 'token' => $token])->one();
        return $app ? $app->authsecret : null;
    }

    public static function getApp($app_id, $token) {
        $app = self::find()->where(['status' => Constant::STATUS_VALID, 'app_id' => $app_id, 'token' => $token])->one();
        return $app ? $app : null;
    }


}
